package com.inool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inool.mapper.GradeMapper;
import com.inool.model.Grade;
import com.inool.model.dto.GradeDto;
import com.inool.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {

    @Autowired
    private GradeMapper gradeMapper;


    

    @Override
    public Grade addGrade(Grade grade) {
        save(grade);
        return grade;
    }

    @Override
    public Grade updateGrade(Long id, Grade grade) {
        grade.setGradeId(id);
        updateById(grade);
        return grade;
    }

    @Override
    public void deleteGrade(Integer id) {
        removeById(id);
    }

    @Override
    public Grade getGradeById(Integer id) {
        return getById(id);
    }

    @Override
    public List<Grade> getAllGrades() {
        return list();
    }

    @Override
    public List<Grade> getGradesByStudentId(Integer studentId) {
        QueryWrapper<Grade> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        return list(wrapper);
    }

    @Override
    public List<Grade> getGradesByCourseId(Integer courseId) {
        QueryWrapper<Grade> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        return list(wrapper);
    }

    @Override
    public Page<GradeDto> pageGrade(long current, long size, String studentName ,String courseName) {
        Page<GradeDto> page = new Page<>(current, size);
        List<GradeDto> dtoList = gradeMapper.pageGrade(page, studentName, courseName);
        page.setRecords(dtoList);
        return page;
    }

    @Override
    public boolean saveOrUpdateGrade(Grade grade) {
        return saveOrUpdate(grade);
    }

    @Override
    public Page<GradeDto> pageStudentGrade(long current, long size,String studentNumber) {
        Page<GradeDto> page = new Page<>(current, size);
        List<GradeDto> dtoList = gradeMapper.pageStudentGrade(page,studentNumber);
        page.setRecords(dtoList);
        return page;
    }
}    